﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using WebQQ2.DLL;

namespace QunRobot
{
    /// <summary>
    /// 2011-1-5
    /// by hackren
    /// Email:hackren@vip.qq.com
    /// </summary>
    public class Common
    {

        private string refMessage;
        private string zhudongMessage;

        public delegate void diaoxian(int uin);
        /// <summary>
        /// 掉线事件
        /// </summary>
        public event diaoxian OnDiaoxian;

        public delegate void dingxiaxian(int uin,string message);
        /// <summary>
        /// 掉线事件
        /// </summary>
        public event dingxiaxian OnDingxiaxian;


        public Users user = null;
        public WebQQCommon Client;

        public Common(Users user)
        {
            this.user = user;
            userEntity entity = new userEntity();
            entity.Username = user.UserName.ToString();
            entity.Password = user.Password;
            Client = new WebQQCommon(entity);
            Client.OnQunMessageEvent += new WebQQCommon.QunMessageHandler(Client_OnQunMessageEvent);
        }

        /// <summary>
        /// 当收到群消息时
        /// </summary>
        /// <param name="groupCode"></param>
        /// <param name="sendTempUin"></param>
        /// <param name="message"></param>
        /// <param name="content"></param>
        void Client_OnQunMessageEvent(string groupCode, string sendTempUin, string message, string content)
        {
            if(Client.user.Quns.Count>0)
            {
                //QunEntity qun1 = Client.user.Quns.Values.ToList()[0];
               // QunEntity qun2 = Client.user.Quns.Values.ToList()[1];

                {
                    QunEntity current = Client.user.Quns[groupCode];
                    string cons = string.Empty;

                    if (current.QunMemberInfo[sendTempUin].TempUin == "20415785")
                    {
                        return;
                    }
                    else if (current.QunMemberInfo[sendTempUin].TempUin == "1166228224")
                    {
                        cons = string.Format("{0} 在群 {1} 中说\\\\r\\\\n{2}",
                          current.QunMemberInfo[sendTempUin].CardName, current.GroupName, "他很II");
                    }
                    else if (current.QunMemberInfo[sendTempUin].TempUin == "1900646994")
                    {
                        cons = string.Format("");
                        return;
                    }
                    else
                    {
                        cons = string.Format("{0} 在群 {1} 中说\\\\r\\\\n",
                         current.QunMemberInfo[sendTempUin].CardName, current.GroupName) + message;
                    }
                    cons.Replace("王飞", "");
                    cons.Replace("飞啊子", "");
                    Client.SendGroupMessage(current.GroupId, cons, content);
                    return;
                }
                //if (groupCode == qun1.GroupCode)
                //{
                //    if (qun1.QunMemberInfo[sendTempUin].CardName != "群消息机器人")
                //    {
                //        string cons = string.Format("{0} 在群 {1} 中说\\\\r\\\\n", qun1.QunMemberInfo[sendTempUin].CardName, qun1.GroupName) + message;
                //        Client.SendGroupMessage(qun2.GroupId, cons, content);
                //    }
                //}
                //else
                //{
                //    if (qun2.QunMemberInfo[sendTempUin].CardName != "群消息机器人")
                //    {
                //        string cons = string.Format("{0} 在群 {1} 中说\\\\r\\\\n", qun2.QunMemberInfo[sendTempUin].CardName, qun2.GroupName) + message;
                //        Client.SendGroupMessage(qun1.GroupId, cons, content);
                //    }
                //}
                //string cons = string.Format("{0} 在群 {1} 中说\\\\r\\\\n", Client.user.Quns[groupCode].QunMemberInfo[sendTempUin].Name, Client.user.Quns[groupCode].GroupName) + message;
                //Client.SendGroupMessage(Client.user.Quns[groupCode].GroupId, cons, content);

            }
            
        }

        /// <summary>
        /// 接收到好友消息
        /// </summary>
        /// <param name="fuin"></param>
        /// <param name="message"></param>
        void Client_OnFriendMessageEvent(string fuin, string message)
        {
            if(refMessage!=null&&refMessage!="")
                Client.sendFriendMessage(fuin, refMessage);
        }

        /// <summary>
        /// 添加好友请求
        /// </summary>
        /// <param name="fuin"></param>
        /// <param name="message"></param>
        void Client_OnAddFriendEvent(string fuin, string message)
        {
            user.AddCount++;
            Client.AddFriend(fuin);
            
            if(zhudongMessage!=null&&zhudongMessage!="")
                Client.sendFriendMessage(fuin, zhudongMessage);
        }

        int scount = 1;
        public void StarLogin()
        {
            if(Client.IsNeedVerifyCode())
            {
                VerifyCodeForm form = new VerifyCodeForm(Client);
                form.ShowDialog();
            }

            if (Client.user.VerifyCode != "")
            {
                Client.login();
                if (Client.user.Status == UserStatus.VerifyCodewang)
                {
                    StarLogin();
                }
                else if (Client.user.Status == UserStatus.Logined)
                {
                    user.State = "登陆成功";
                    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
                    ThreadPool.QueueUserWorkItem(new WaitCallback(BeginSendGroupMessage));
                    Client.ChangeStatus(getStatus());
                    Client.InitQunList();
                }
                else
                {
                    user.State = "登陆失败";
                }
            }else
            {
                user.State = "取消登陆";
            }
        }


        /// <summary>
        /// 线程池子程序
        /// </summary>
        /// <param name="stateInfo"></param>
        void ThreadProc(Object username)
        {
            while (true)
            {
                Client.GetMessage();
            }
        }

        void BeginSendGroupMessage(object a)
        { 
            
        }

        private OnlineStatusEnum getStatus()
        {
            switch (user.OnLoneStatus)
            {
                case "离线":
                    return OnlineStatusEnum.Offline;
                    break;
                case "在线":
                    return OnlineStatusEnum.Online;
                    break;
                case "隐身":
                    return OnlineStatusEnum.Hidden;
                    break;
                case "忙碌":
                    return OnlineStatusEnum.Busy;
                    break;
                case "离开":
                    return OnlineStatusEnum.Away;
                    break;
                case "Q我吧":
                    return OnlineStatusEnum.Callme;
                    break;
                case "请勿打扰":
                    return OnlineStatusEnum.Silent;
                    break;
                default:
                    return OnlineStatusEnum.Online;
                    break;
            }
        }

        public void changeState(){}
    }
}
